package com.zzccyy.jpdemo.base

import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.sztle.admodule.outstatus.AppParamBuilder
import com.sztle.admodule.outstatus.AppStatusProcessor
import com.zzccyy.jpdemo.BuildConfig
import com.zzccyy.jpdemo.page.SplActivity
import me.jessyan.autosize.AutoSizeConfig
import me.jessyan.autosize.onAdaptListener

class BaseApplication : Application() {

    var isBackground: Boolean = false
    var activityCount: Int = 0

    override fun onCreate() {
        super.onCreate()
        context = this


        //todo list 初始化并传入渠道、各个广告id及其他所需id
        val appParam = AppParamBuilder()
            .setChannel(BuildConfig.FLAVOR) //商店渠道
            .setDebug(BuildConfig.DEBUG) // 是否是debug模式,正式发布一定是要false
            .setGremoreAppId("5751230") // gremore聚合appid
            .setUMengAppKey("690061d1644c9e2c20678ef4") // 友盟appkey
            .setAdsparkAppId("855418") // AdSpark的appid
            .setSplashAdId("103692587") // 开屏广告id
            .setInterstitialAdId("103692486") // 插屏广告id
            .setRewardedVideoAdId("103692293") // 激励视频广告id
            .setNativeAdId("103694426") // 原生广告id
            .build()
        AppStatusProcessor.statusStartApp(this, appParam)

        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
            }

            override fun onActivityStarted(activity: Activity) {
                activityCount++
                if (isBackground && activity !is SplActivity) {
                    val intent = Intent(activity, SplActivity::class.java)
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    intent.putExtra("isFromAwake", true)
                    activity.startActivity(intent)
                    activity.overridePendingTransition(0, 0)
                }
                isBackground = false
                AppStatusProcessor.setBackground(false)
            }

            override fun onActivityResumed(activity: Activity) {
                if (activity is BasicBaseActivity) {
                    AppStatusProcessor.setTopActivity(activity)
                }
            }

            override fun onActivityPaused(activity: Activity) {
            }

            override fun onActivityStopped(activity: Activity) {
                activityCount--
                if (activityCount <= 0) {
                    isBackground = true
                    activityCount = 0
                    AppStatusProcessor.setBackground(true)
                }
            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
            }

            override fun onActivityDestroyed(activity: Activity) {
            }
        })

        AutoSizeConfig.getInstance() //是否让框架支持自定义 Fragment 的适配参数, 由于这个需求是比较少见的, 所以须要使用者手动开启
            //如果没有这个需求建议不开启
            .setCustomFragment(true) //是否屏蔽系统字体大小对 AndroidAutoSize 的影响, 如果为 true, App 内的字体的大小将不会跟随系统设置中字体大小的改变
            //如果为 false, 则会跟随系统设置中字体大小的改变, 默认为 false
            .setExcludeFontScale(true) //屏幕适配监听器
            .setOnAdaptListener(object : onAdaptListener {
                override fun onAdaptBefore(target: Any, activity: Activity) {
                }

                override fun onAdaptAfter(target: Any, activity: Activity) {
                }
            })
    }

    companion object {
        var context: Context? = null
    }
}